Module BrendanFunctions
    Sub RegisterFunctions(ByRef funcList As FunctionList)
        funcList.RegisterFunction("write", AddressOf consolewrite)
        funcList.RegisterFunction("read", AddressOf consoleread)
        funcList.RegisterFunction("add", AddressOf consoleadd)
        funcList.RegisterFunction("rnd", AddressOf consolernd)
        funcList.RegisterFunction("subtract", AddressOf consolesubtract)
        funcList.RegisterFunction("goto", AddressOf consolegoto)
        funcList.RegisterFunction("multiply", AddressOf consolemultiply)
        funcList.RegisterFunction("divide", AddressOf consoledivide)
    End Sub
    Function consolewrite(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 1 Then Return False 'do not run function if less than 1 argument
        Console.WriteLine(args.Item(0).Data) 'write the data from argument 0 to the screen
        Return True 'return function as executed
    End Function
    Function consoleread(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 1 Then Return False ' do no run function if less than 1 argument
        args.Item(0).Data = Console.ReadLine() 'reads the line that is typed in argument 0
        Return True 'return function as executed
    End Function
    Function consoleadd(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'do not run function if less than 3 arguments
        args.Item(2).Data = args.Item(0) + args.Item(1) 'argument 2 is arguments 1 and 2 added together
        Return True 'return function as executed
    End Function
    Function consolernd(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'do not run function if less than 3 arguments
        Randomize() 'randomize and set arguments 1 and 2 to min/max variables
        Dim max As Integer = args.Item(1).Data
        Dim min As Integer = args.Item(0).Data
        args.Item(2).Data = ((max - min + 1) * Rnd() + min) 'argument 2 = a random number generated using the min/max
        Return True 'return function as executed
    End Function
    Function consolesubtract(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'if less than 3 arguments do not run function
        args.Item(2).Data = args.Item(0) - args.Item(1) 'argument 2 = argument 0 minus argument 1
        Return True 'return function as executed
    End Function
    Function consolegoto(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 1 Then Return False 'if less than 1 function do not run
        script.SetInstructionPointer(args.Item(0).Data) ' set instruction pointer to line in argument 0
        Return True ' return function as executed
    End Function
    Function consolemultiply(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False ' if less than 3 arguments do not run
        args.Item(2).Data = args.Item(0) * args.Item(1) 'argument 2 equals argument 0 times argument 1
        Return True 'return function as executed
    End Function
    Function consoledivide(ByVal args As ArgList, ByRef script As Script) As Boolean
        If args.Count < 3 Then Return False 'if less than 3 arguments do not run
        args.Item(2).Data = args.Item(0) / args.Item(1) 'argument 2 equals argument 0 divided by argument 1
        Return True 'return function as executed
    End Function
End Module
